package com;



import java.io.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Set;

public class CheckNeed {

    private static String sp = File.separator;

    public static void main(String[] args) throws IOException {
        String packagepath = "C:\\Users\\Administrator\\Desktop\\animal2";
        String mobanpath = "E:\\projects\\GenHtml2.0\\src\\main\\resources\\" + "animal-PacBio" + ".properties";
        ArrayList<String> check = check(packagepath, mobanpath);
        System.out.println(check);
    }


    public static ArrayList<String> check(String packagepath, String mobanpath) throws IOException {
        packagepath = change(packagepath);
        mobanpath = change(mobanpath);
        Properties pp = new Properties();
        pp.load(new FileReader(mobanpath));
        return checkmain(packagepath, pp);
    }

    private static ArrayList<String> checkmain(String packagepath, Properties pp) {

        ArrayList<String> result = new ArrayList<String>();
        Set<String> strings = pp.stringPropertyNames();
        ArrayList<String> checkpackage = checkpackage(packagepath);
        if (checkpackage.size()!=0) {
            return checkpackage;
        }

        for (String s : strings) {
            ArrayList<String> bb  = checkone(packagepath, pp.get(s).toString());
            if(bb.size() >0){
                result.addAll(bb);
            }
        }
        return result ;
    }

    private static ArrayList<String> checkone(String packagepath, String s) {

        ArrayList<String> bb = new ArrayList<String>();
        String s1 = packagepath + sp + s;
        File file = new File(s1);
        String packageresult = packagepath;
        if (s.contains("*")) {
            String[] split = s.split("/");
            int flenggth = split.length - 1;
            for (int a = 0; a < flenggth; a++) {
                if (split[a].equals("*")) {
                    File file1 = new File(packageresult);
                    File[] files = file1.listFiles();
                    String zz = packageresult;
                    if(files == null){
                        break;}
                        for (File ff : files) {
                            if (ff.exists() && ff.isDirectory()) {
                                packageresult = ff.getAbsolutePath();
                                break;
                            }
                        }
                    if (packageresult.equals(zz)&& new File(packageresult).exists() ){
                            System.out.println("--warning: " + packageresult+ "  Missing necessary folders");
                            bb.add("--warning: " + packageresult+ "  Missing necessary folders");
                        }

                } else {
                    packageresult = new File(packageresult + sp + split[a]).getAbsolutePath();
                    if (!new File(packageresult).exists()) {
                        System.out.println("--warning:No folder exists " + packageresult);
                        bb.add("--warning:No folder exists " + packageresult);
                    }
                }
            }

            String s2 = split[flenggth];
            if (s2.contains("*")) {
                String s3 = s2.replace("*", "");
                File[] files = new File(packageresult).listFiles();
                String zz = packageresult;
                if(files == null){
                    System.out.println("--warning:No folder exists " + packageresult);
                    bb.add("--warning:No folder exists " + packageresult);
                    return bb;}
                for (File fg : files) {
                    String name = fg.getName();
                    if (fg.exists() && !fg.isDirectory() && cc(name, s3)) {
                        packageresult = fg.getAbsolutePath();
                        break;
                    }
                }
                if (packageresult.equals(zz)) {
                    System.out.println("--warning: " + packageresult + "  Missing under folder" + split[flenggth]);
                    bb.add("--warning: " + packageresult + "  Missing under folder" + split[flenggth]);
                }
            } else {
                packageresult = new File(packageresult + sp + s2).getAbsolutePath();
            }
        } else {
            if (!file.exists()) {
                System.out.println("--warning:No folder exists" + s);
                bb.add("--warning:No folder exists" + s);
            }
        }
        return bb;
    }

    private static boolean cc(String name, String s3) {
        int namelength = name.length();
        int s3length = s3.length();
        if (namelength >= s3length) {
            String namefanzhuan = fanzhuan(name).substring(0, s3length);
            String s3fanzhuan = fanzhuan(s3);
            if (namefanzhuan.equals(s3fanzhuan)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    private static ArrayList<String> checkpackage(String packagepath) {
        ArrayList<String> listresult = new ArrayList<String>();
        int result = 0;
        ArrayList<String> strings = new ArrayList<String>();
        strings.add("01.RawDataQC");
        strings.add("02.Assembly");
        strings.add("03.Genome_Component");
        strings.add("04.Genome_Annotation");
        strings.add("05.Genome_Map");
        for (String s : strings) {
            String pp = packagepath + sp + s;
            File file = new File(pp);
            if (!(file.exists() && file.isDirectory())) {
                result = 1;
                System.out.println("--No folder exists: " + s);
                listresult.add("--No folder exists: " + s);
            }
        }
        if (result != 1) {
            System.out.println("--Original folder complete");
        } else {
            System.out.println("--warning:Folders are not matched");
        }
        return listresult;
    }

    private static String change(String path) {
        path = path.replace("/", sp);
        path = path.replace("\\", sp);
        return path;
    }

    private static String fanzhuan(String s) {
        return new StringBuilder(s).reverse().toString();
    }

}
